<?php
/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Ev
 * @package    Ev_View
 * @subpackage Helper
 * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @version    $Id: FormMultiSelect.php 16541 2009-07-07 06:59:03Z bkarwin $
 */


/**
 * Abstract class for extension
 */
require_once 'Zend/View/Helper/FormElement.php';


/**
 * Helper to generate "multiple select" list of options
 *
 * @category   Zend
 * @package    Zend_View
 * @subpackage Helper
 * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */
class Ev_View_Helper_FormDoubleSelect extends Zend_View_Helper_FormElement
{
    /**
     * Generates 'milti select' list of options.
     *
     * @access public
     *
     * @param string|array $name If a string, the element name.  If an
     * array, all other parameters are ignored, and the array elements
     * are extracted in place of added parameters.
     *
     * @param mixed $value The option value to mark as 'selected'; if an
     * array, will mark all values in the array as 'selected' (used for
     * multiple-select elements).
     *
     * @param array|string $attribs Attributes added to the 'select' tag.
     *
     * @param array $options An array of key-value pairs where the array
     * key is the radio value, and the array value is the radio text.
     *
     * @param string $listsep When disabled, use this list separator string
     * between list values.
     *
     * @return string The select tag and options XHTML.
     */
    public function formDoubleSelect( $iComboOrigem, $iComboDestino, $aOptionTotal = array( '' => 'Selecione' ), $aOptionSelecionado = array(), $sBaseUrl, $aAtributos = array(), $listsep = "<br />\n" )
    {
        $value = array_map('strval', (array) $value);

        $disable = array();
        // check if element may have multiple values
        $multiple = '';

        if (substr($name, -2) == '[]') {
            // multiple implied by the name
            $multiple = ' multiple="multiple"';
        }

        if (isset($attribs['multiple'])) {
            // Attribute set
            if ($attribs['multiple']) {
                // True attribute; set multiple attribute
                $multiple = ' multiple="multiple"';

                // Make sure name indicates multiple values are allowed
                if (!empty($multiple) && (substr($name, -2) != '[]')) {
                    $name .= '[]';
                }
            } else {
                // False attribute; ensure attribute not set
                $multiple = '';
            }
            unset($attribs['multiple']);
        }

        // now start building the XHTML.
        $disabled = '';
        if (true === $disable) {
            $disabled = ' disabled="disabled"';
        }

        // Build the surrounding select element first.
        $xhtml = '<select'
                . ' multiple="multiple"'
                . ' size="7"'
                . ' style="width: 200px;"'
                . $disabled
                . $this->_htmlAttribs($aAtributos);
//                . ">\n    ";

        // build the list of options
        $listTotal           = array();
        $listSelecionados   = array();
        $translator = $this->getTranslator();
        foreach ((array) $aOptionTotal as $opt_value => $opt_label) {
            if (is_array($opt_label)) {
                $opt_disable = '';
                if (is_array($disable) && in_array($opt_value, $disable)) {
                    $opt_disable = ' disabled="disabled"';
                }
                if (null !== $translator) {
                    $opt_value = $translator->translate($opt_value);
                }
                $list[] = '<optgroup'
                        . $opt_disable
                        . ' label="' . $this->view->escape($opt_value) .'">';
                foreach ($opt_label as $val => $lab) {
                    $list[] = $this->_build($val, $lab, $value, $disable);
                }
                $list[] = '</optgroup>';
            } else {
                if( in_array( $opt_value, $aOptionSelecionado ) ){
                    $listSelecionados[] = $this->_build($opt_value, $opt_label, $disable);
                }
                else{
                    $listTotal[] = $this->_build($opt_value, $opt_label, $disable);
                }
            }
        }

        // add the options to the xhtml and close the select
        $xhtmlTotal         = $xhtml.' id="' . $this->view->escape($iComboOrigem)  . '" name="' . $this->view->escape($iComboOrigem) . '[]" onDblClick="moveSelectedOptions( document.getElementById( \''.$iComboOrigem.'\' ), document.getElementById( \''.$iComboDestino.'\' ), true, \'\' );" '. ">\n    ".implode("\n    ", $listTotal) . "\n</select>";
        $xhtmlSelecionado     = $xhtml.' id="' . $this->view->escape($iComboDestino) . '" name="' . $this->view->escape($iComboDestino) . '[]"onDblClick="moveSelectedOptions( document.getElementById( \''.$iComboDestino.'\' ), document.getElementById( \''.$iComboOrigem.'\' ), true, \'\' );" sortSelect( document.getElementById( \''.$iComboOrigem.'\' ) );" doubleselect="true" '. ">\n    ".implode("\n    ", $listSelecionados) . "\n</select>";

        $sComponente =     '<table>
                            <tr valign="middle">
                                <td>
                                    Itens Não Selecionados<br />
                                    '.$xhtmlTotal.'
                                </td>
                                <td>
                                    <img src="'.$sBaseUrl.'/img/doubleSelect/rarrow_one.gif" style="padding: 2px" onclick="moveSelectedOptions( document.getElementById( \''.$iComboOrigem.'\' ), document.getElementById( \''.$iComboDestino.'\' ), true, \'\' );"/><br/>
                                    <img src="'.$sBaseUrl.'/img/doubleSelect/rarrow_all.gif" style="padding: 2px" onclick="moveAllOptions( document.getElementById( \''.$iComboOrigem.'\' ), document.getElementById( \''.$iComboDestino.'\' ), true, \'\' );"/><br/>
                                    <img src="'.$sBaseUrl.'/img/doubleSelect/larrow_all.gif" style="padding: 2px" onclick="moveAllOptions( document.getElementById( \''.$iComboDestino.'\' ), document.getElementById( \''.$iComboOrigem.'\' ), true, \'\'); sortSelect( document.getElementById( \''.$iComboOrigem.'\' ) );"/><br/>
                                    <img src="'.$sBaseUrl.'/img/doubleSelect/larrow_one.gif" style="padding: 2px" onclick="moveSelectedOptions( document.getElementById( \''.$iComboDestino.'\' ), document.getElementById( \''.$iComboOrigem.'\' ), true, \'\' ); sortSelect( document.getElementById( \''.$iComboOrigem.'\' ) );"/><br/>
                                </td>
                                <td>
                                    Itens Selecionados<br />
                                    '.$xhtmlSelecionado.'
                                </td>
                            </tr>
                        </table>';
        return $sComponente;
    }

    /**
     * Builds the actual <option> tag
     *
     * @param string $value Options Value
     * @param string $label Options Label
     * @param array  $selected The option value(s) to mark as 'selected'
     * @param array|bool $disable Whether the select is disabled, or individual options are
     * @return string Option Tag XHTML
     */
    protected function _build($value, $label, $disable)
    {
        if (is_bool($disable)) {
            $disable = array();
        }

        $opt = '<option'
             . ' value="' . $this->view->escape($value) . '"'
             . ' label="' . $this->view->escape($label) . '"';

        // disabled?
        if (in_array($value, $disable)) {
            $opt .= ' disabled="disabled"';
        }

        $opt .= '>' . $this->view->escape($label) . "</option>";

        return $opt;
    }
}
